import router from "@src/router";
import { getQrInfo } from "@src/api/base";
import { matchCardCode } from '@src/common/coupon';
Component({
  data: {
    options: {},
    loading: true,
  },
  methods: {
    onLoad(options) {
      console.log('options', options)
      this.setData({ options });
    },
    onLogin() {
      let self = this;
      if (this.data.options.q) {
        let url = decodeURIComponent(this.data.options.q);
        console.log("transfer-options-url", url);
        let index = url.lastIndexOf("/");
        let codeNo = url.substring(index + 1, url.length);
        //跳转卡券
        let flag = matchCardCode(url);
        if (flag) {
          setTimeout(() => {
            self.setData({ loading: false })
            router.push({ name: 'member/exchangeCoupon', params: { code: flag.id, type: flag.type } })
          }, 1000)

          return;
        }
        self.baseQr(codeNo);
      } else {
        // 无数据
        this.setData({ loading: false })
        router.push({ name: 'index' })
      }
    },
    //获取动态会员码
    baseQr(codeNo) {
      let data = {
        codeNo: codeNo
      };
      let self = this;
      getQrInfo(data).then(res => {
        self.setData({ loading: false })
        let errMsg = "";
        let qrInfo = res.data;
        if (!qrInfo) {
          errMsg = "无效识别码";
        } else if (qrInfo.status === 0) {
          errMsg = "识别码已停用";
        } else if (qrInfo.bindStatus === 0) {
          errMsg = "识别码未绑定";
        } else if (qrInfo.bizType) {
          switch (qrInfo.bizType) {
            case 1:
              router.push({
                name: "catering/index",
                params: {
                  shopId: qrInfo.bizShopId,
                  tableNo: qrInfo.bizNo
                }
              })
              return;
            default:
              errMsg = "请扫描点餐识别码";
              break;
          }
        } else {
          errMsg = "无效识别码";
        }
        // 二维码异常跳转首页
        wx.showModal({
          title: "温馨提示",
          content: errMsg,
          showCancel: false,
          success(res) {
            router.push({ name: "index" });
          }
        });
      }).catch(res => {
        self.setData({ loading: false })
        wx.showModal({
          title: "温馨提示",
          content: "请求异常",
          showCancel: false,
          success(res) {
            router.push({ name: "index" });
          }
        });
      })
    }
  },
})
